package ru.ifmo.swing.genetics.monitoring.common;

import org.uncommons.watchmaker.framework.EvolutionObserver;
import org.uncommons.watchmaker.framework.PopulationData;
import ru.ifmo.swing.genetics.monitoring.common.SwingEvolutionObserver;

import javax.swing.*;
import java.util.Collection;
import java.util.Map;

/**
 * @author avhaliullin
 */
public class AdvancedEvolutionMonitor<T> implements SwingEvolutionObserver<T> {
    private final JTabbedPane view;
    private final Collection<? extends EvolutionObserver<T>> observers;

    public AdvancedEvolutionMonitor(Map<String, SwingEvolutionObserver<T>> tabs) {
        view = new JTabbedPane();
        for (Map.Entry<String, SwingEvolutionObserver<T>> tab : tabs.entrySet()) {
            view.addTab(tab.getKey(), tab.getValue().getView());
        }
        observers = tabs.values();
    }

    public void populationUpdate(final PopulationData<? extends T> populationData) {
//        if (SwingUtilities.isEventDispatchThread()) {
            for (EvolutionObserver<T> observer : observers) {
                observer.populationUpdate(populationData);
            }
//        } else {
//            SwingUtilities.invokeLater(new Runnable() {
//                public void run() {
//                    populationUpdate(populationData);
//                }
//            });
//        }
    }

    public JComponent getView() {
        return view;
    }
}
